﻿using System.Reflection;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using NHibernate;
using Scribble.Repositories;

namespace Scribble.Web
{
    public class Container
    {
        public void Configure()
        {
            // TODO: Understand what's happening here
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetCallingAssembly());

            builder.RegisterAssemblyTypes(typeof(PostRepository).Assembly).AsImplementedInterfaces();

            builder.Register(c => new NHibernateConfigurator().GetSessionFactory()).SingleInstance();
            builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()).InstancePerHttpRequest();

            builder.RegisterModule(new AutofacWebTypesModule());
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}